package com.eam.handler;

import com.eam.data.EamSpareparts;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.client.kernel.BaseActionHandler;
import org.openbravo.dal.service.OBDal;
import java.util.List;
import java.util.Map;

/**
 * 设备备品配件盘点查询数据
 */
public class SparepartInventoryItemQueryHandler  extends BaseActionHandler {
	@Override
	protected JSONObject execute(Map<String, Object> parameters, String data) {
		JSONObject jsonRep = new JSONObject();
		try {
			JSONObject jsonData = new JSONObject(data);
			String strId = jsonData.getString("ids");
			//
			String[] ids = strId.split("#####");
			// 物料
			jsonRep.put("reqData", getCallPutMaterial(ids));

			//查询所有设备备品配件物料仓库下的储位
			//String qq = "select barcode   from M_locator  where M_locator_id in (select M_locator_id  from Eam_Spareparts_Collar ckjl  where ckjl.Eam_Spareparts_id = (select Eam_Spareparts_id from Eam_Spareparts where code = '010103041637') )";

			/*String sql = "select new map(id as id, barcode as barcode) from Locator  where id in (select locator.id from Eam_Spareparts_Collar where  id = (select id from Eam_Spareparts where code = ? )) ";
			List<Map<String, String>> list2 = OBDal.getInstance().getSession().createQuery(sql).setString(o, arg1).list();
			JSONObject LocatorMap = new JSONObject();
			for(Map<String, String> map : list2) {
				LocatorMap.put(map.get("id"), map.get("barcode"));
			}

			jsonRep.put("LocatorMap", LocatorMap);*/

			jsonRep.put("result", true);
			OBDal.getInstance().commitAndClose();
		} catch (Exception e) {
			OBDal.getInstance().rollbackAndClose();
			e.printStackTrace();
			try {
				jsonRep.put("result", false);
				jsonRep.put("msg", "查询失败：" + e.getMessage());
			} catch (Exception ex) {
			}
		}
		return jsonRep;
	}

	private JSONArray getCallPutMaterial(String[] ids) throws Exception {
		JSONArray data = new JSONArray();
		for (String id : ids) {
			EamSpareparts Spare = OBDal.getInstance().get(EamSpareparts.class, id);
			JSONObject obj = new JSONObject();
			obj.put("ASN_CODE", Spare.getCode());											//物料编码
			obj.put("M_NAME", Spare.getName());												//物料名称
			obj.put("M_MODEL", Spare.getModel());											//规格型号

			obj.put("Remaining", Spare.getSafetyStock());									//安全库存数量

			String qq = "select barcode  from M_locator  where M_locator_id in (select M_locator_id  from Eam_Spareparts_Collar ckjl  where ckjl.Eam_Spareparts_id = (select Eam_Spareparts_id from Eam_Spareparts where code = ? ) )";

			List<Map<String, Object>> barcode = (List<Map<String, Object>>)OBDal.getInstance().getSession().createSQLQuery(qq).setString(0, Spare.getCode()).list();

			obj.put("M_Locator", barcode);

			String companys = "";

			if( Spare.getCompany().equals("10")){
				companys= "对";
			}else if( Spare.getCompany().equals("20")){
				companys= "个";
			}else if( Spare.getCompany().equals("30")){
				companys= "根";
			}else if( Spare.getCompany().equals("40")){
				companys= "盒";
			}else if( Spare.getCompany().equals("50")){
				companys= "卷";
			}else if( Spare.getCompany().equals("60")){
				companys= "块";
			}else if( Spare.getCompany().equals("70")){
				companys= "片";
			}else if( Spare.getCompany().equals("80")){
				companys= "套";
			}

			if( Spare.getEmesProcessopr()  == null){

			}else{
				obj.put("processopr", Spare.getEmesProcessopr().getName());											//厂房区域
			}
			obj.put("company",companys);											//单位

			obj.put("PlanIds", Spare.getId());												//行ID
			obj.put("remarks", "");
			data.put(obj);
		}
		return data;
	}
}